package com.future.mayi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.HttpRequest;
import com.future.baselib.utils.statusbarlibrary.StatusBarUtils;
import com.future.mayi.R;
import com.future.mayi.adapter.InviteAdapter;
import com.future.mayi.app.App;
import com.future.mayi.response.InviteResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InviteActivity extends BaseActivity implements XRecyclerView.LoadingListener {


    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_invite_code)
    TextView tvInviteCode;
    @BindView(R.id.tv_invite_copy)
    TextView tvInviteCopy;
    @BindView(R.id.tv_invite_code_img)
    TextView tvInviteCodeImg;
    @BindView(R.id.tv_all_num)
    TextView tvAllNum;
    @BindView(R.id.tv_tuan_num)
    TextView tvTuanNum;


    private InviteAdapter adapter;

    @Override
    protected void initLayout() {
        StatusBarUtils.setStatusBarColor(getWindow(), getResources().getColor(R.color.main_color), false);
        setContentView(R.layout.activity_invite);
    }

    @Override
    protected void initView() {
        adapter = new InviteAdapter(mContext, recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingMoreEnabled(false);
    }

    @Override
    protected void initDates() {


    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
        recyclerView.refresh();
    }


    @Override
    public void onRefresh() {
        getData();
    }

    @Override
    public void onLoadMore() {

    }


    private void getData() {
        new HttpRequest<InviteResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.invite)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<InviteResponse>() {

                    @Override
                    public void onSuccess(InviteResponse response) {
                        if (response.model != null) {
                            tvInviteCode.setText(response.model.getInvite_code() == null ? "" : response.model.getInvite_code());
                            tvAllNum.setText(String.valueOf(response.model.getUser().getTotal()));
                            tvTuanNum.setText(String.valueOf(response.model.getUser().getAgent()));
                            HCUtils.refreshListForPage(recyclerView, adapter, response.model.getList(), 0, 20);
                        } else HCUtils.refreshListForPage(recyclerView, adapter, null, 0, 20);
                    }

                    @Override
                    public void onFail(String message, InviteResponse response) {
                        HCUtils.refreshFail(recyclerView, 0, mContext, response);
                    }
                }).start(new InviteResponse());


    }


    @OnClick({R.id.iv_title_left, R.id.tv_invite_code, R.id.tv_invite_copy, R.id.tv_invite_code_img})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                onBackPressed();
                break;
            case R.id.tv_invite_code:
            case R.id.tv_invite_copy:
                if (TextUtils.isEmpty(tvInviteCode.getText())) return;
                CommonUtils.clipData(mContext, tvInviteCode.getText().toString());
                toast("复制成功");
                break;
            case R.id.tv_invite_code_img:
                startActivity(new Intent(mContext, MyQrCodeActivity.class));
                break;
        }
    }


}
